#include"Stack.h"


typedef struct {
    Stack s1;
    Stack s2;

} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* q = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&q->s1);
    StackInit(&q->s2);
    return q;

}

void myQueuePush(MyQueue* obj, int x) {
    if (!StackEmpty(&obj->s1))
        StackPush(&obj->s1, x);
    else
        StackPush(&obj->s2, x);
}

int myQueuePop(MyQueue* obj) {
    Stack* empty = &obj->s1;
    Stack* nonempty = &obj->s2;
    if (StackEmpty(&obj->s2))
    {
        empty = &obj->s2;
        nonempty = &obj->s1;
    }
    while (StackSize(nonempty) > 1)
    {
        StackPush(empty, StackTop(nonempty));
        StackPop(nonempty);
    }
    int top = StackTop(nonempty);
    StackPop(nonempty);
    while (StackSize(empty) > 0)
    {
        StackPush(nonempty, StackTop(empty));
        StackPop(empty);
    }
    return top;
}

int myQueuePeek(MyQueue* obj) {
    Stack* empty = &obj->s1;
    Stack* nonempty = &obj->s2;
    if (StackEmpty(&obj->s2))
    {
        empty = &obj->s2;
        nonempty = &obj->s1;
    }
    while (StackSize(nonempty) > 1)
    {
        StackPush(empty, StackTop(nonempty));
        StackPop(nonempty);
    }
    int top = StackTop(nonempty);
    StackPush(empty, StackTop(nonempty));
    StackPop(nonempty);
    while (StackSize(empty) > 0)
    {
        StackPush(nonempty, StackTop(empty));
        StackPop(empty);
    }
    return top;
}

bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->s1) && StackEmpty(&obj->s2);
}

void myQueueFree(MyQueue* obj) {
    StackDestroy(&obj->s1);
    StackDestroy(&obj->s2);
    free(obj);
}
int main()
{
    MyQueue* obj = myQueueCreate();
    myQueuePush(obj, 1);
    myQueuePush(obj, 1);
    myQueuePush(obj, 3);

    int param_2 = myQueuePop(obj);

    int param_3 = myQueuePeek(obj);

    bool param_4 = myQueueEmpty(obj);

    printf("%d %d %d\n", param_2, param_3, param_4);
    myQueueFree(obj);
	
	return 0;
}